package cc.comac.mom.util;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.jackrain.nea.sys.domain.User;
import org.springframework.stereotype.Component;

import cc.comac.mom.entity.base.BaseEntity;
import cc.comac.mom.redis.IdFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

//@ComponentScan("com.efast.dmom.mes.util")
//@Configuration
//@EnableAspectJAutoProxy
@Component
public class BaseEntityDataUtil {
	@Resource
	private IdFactory idFactory;

	
	public long getGenerateId() {
		long generateId = idFactory.generateId();
		return generateId;
	}
	
	public void setBaseEntityData(BaseEntity entity) {
		if(entity==null) {
			return;
		}
		entity.setId(idFactory.generateId());
	//	entity.setSiteId(idUtil.getSiteId());
		entity.setCreationTime(TimeUtil.getLocalDateTime());
		entity.setCreationTimeZone(TimeUtil.getLocalTimeZone());
		entity.setCreator(getUserName());
		entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		entity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		entity.setLastModifier(getUserName());
		entity.setRowStatus(1);
	//	entity.setRowTrxId(idUtil.getTrxId());
		return;
	}
	
	public void setBaseEntityData(List<BaseEntity>  listEntity) {
		for(BaseEntity entity:listEntity) {
			setBaseEntityData(entity);
		}
		
	}
	
	public void updateBaseEntityData(BaseEntity  entity) {
		if(entity==null) {
			return ;
		}
		entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		entity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		entity.setLastModifier(getUserName());
	//	entity.setLastModifiedPid(idUtil.getDmomPid());
	//	entity.setRowTrxId(idUtil.getTrxId());
	}
	public void updateBaseEntityData(List<BaseEntity>  listEntity) {
		for(BaseEntity entity:listEntity) {
			updateBaseEntityData(entity);
		}
	}
	
	public void deleteBaseEntityData(BaseEntity  entity) {
		if(entity==null) {
			return ;
		}
		entity.setLastModifiedTime(TimeUtil.getLocalDateTime());
		entity.setLastModifiedTimeZone(TimeUtil.getLocalTimeZone());
		entity.setLastModifier(getUserName());
		entity.setRowStatus(0);
	//	entity.setLastModifiedPid(idUtil.getDmomPid());
	//	entity.setRowTrxId(idUtil.getTrxId());
	}
	public void deleteBaseEntityData(List<BaseEntity>  listEntity) {
		for(BaseEntity entity:listEntity) {
			deleteBaseEntityData(entity);
		}
	}
	
	public String getStringTime () {
		DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
		String format = ofPattern.format(LocalDateTime.now());
		return format;
	}
	public String getCurrentUser () {
		return getUserName();
	}
	
	public String getJobNumber() {
		return getUser().getName();
	}

	private User getUser(){
		HttpServletRequest request=((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest();
		//HttpSession session=request.getSession();
		User user=(User)request.getAttribute("user");
		if(user==null){
			user=new User();
			user.setName("system");
			user.setTrueName("local");
		}
		return user;
	}
	private String getUserName(){
		return getUser().getTrueName();
	}


}